home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 126-150 / disk_126 / dance / dance1.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  6KB  |  263 lines

  1. /****************************************************************
  2.  * Polygon drawing demo by John M. Olsen
  3.  * This demo uses the AreaMove, AreaDraw and AreaEnd Functions.
  4.  *
  5.  * My appologies for the sparceness of comments.  It is hopefully clear
  6.  * what I was doing for the most part.
  7.  *
  8.  * This is meant to be compiled with the Manx compiler, and has not been
  9.  * tried under Lettuce.
  10.  *
  11.  * This software is in the public domain, and you can do whatever you want
  12.  * to or with it.
  13.  *
  14.  * John M. Olsen
  15.  * 1547 Jamestown Drive
  16.  * Salt Lake City, UT  84121
  17.  ****************************************************************/
  18.  
  19. #include <exec/types.h>
  20. #include <graphics/gfxbase.h>
  21. #include <graphics/gfxmacros.h>
  22. #include <graphics/rastport.h>
  23. #include <intuition/intuition.h>
  24. #include <stdio.h>
  25.  
  26. void *OpenLibrary();
  27. struct Window *OpenWindow(), *w, *w2;
  28. struct Screen *OpenScreen(), *s;
  29. struct IntuiMessage *GetMsg(), *msg;
  30.  
  31. struct IntuitionBase *IntuitionBase;
  32. struct GfxBase *GfxBase;
  33.  
  34. BYTE *AllocRaster();
  35. WORD areabuffer[250];
  36. struct TmpRas tmpras;
  37. struct AreaInfo myAreaInfo;
  38.  
  39. USHORT a[] = { 0x9429, 0x4294 };
  40. USHORT b[] = { 0x4294, 0x2942 };
  41.  
  42. /*****************************************************************
  43. definition of the 320 X 200 screen.
  44. *****************************************************************/
  45. struct NewScreen ss =
  46. {                /*****************/
  47.     0,            /* LeftEdge      */
  48.     0,            /* TopEdge       */
  49.     320,            /* Width         */
  50.     200,            /* Height        */
  51.     5,            /* Depth         */
  52.     0,            /* DetailPen     */
  53.     1,            /* BlockPen      */
  54.     NULL,            /* ViewModes     */
  55.     CUSTOMSCREEN,        /* Type          */
  56.     NULL,            /* *Font         */
  57.     (UBYTE *) "A Dancing Polygon",    /* *DefaultTitle */
  58.     NULL,            /* *Gadgets      */
  59.     NULL            /* *CustomBitMap */
  60. };                /*****************/
  61.  
  62. struct NewWindow ww =
  63. {                /****************/
  64.     0,            /* LeftEdge    */
  65.     10,            /* TopEdge    */
  66.     320,            /* Width    */
  67.     190,            /* Height    */
  68.     -1,            /* DetailPen    */
  69.     -1,            /* BlockPen    */
  70.     CLOSEWINDOW,        /* IDCMP    */
  71.     WINDOWCLOSE | ACTIVATE | NOCAREREFRESH | WINDOWDRAG,
  72.     NULL,            /* *FirstGadget    */
  73.     NULL,            /* *CheckMark    */
  74.     (UBYTE *)"By John M. Olsen",    /* *Title    */
  75.     NULL,            /* Screen    */
  76.     NULL,            /* *Bitmap    */
  77.     0,0,0,0,        /* Min/Max w,h    */
  78.     CUSTOMSCREEN        /* Type        */
  79. };
  80. struct NewWindow ww2 =
  81. {                /****************/
  82.     0,            /* LeftEdge    */
  83.     0,            /* TopEdge    */
  84.     320,            /* Width    */
  85.     190,            /* Height    */
  86.     -1,            /* DetailPen    */
  87.     -1,            /* BlockPen    */
  88.     NULL,            /* IDCMP    */
  89.     BACKDROP | NOCAREREFRESH,
  90.     NULL,            /* *FirstGadget    */
  91.     NULL,            /* *CheckMark    */
  92.     (UBYTE *)NULL,        /* *Title    */
  93.     NULL,            /* Screen    */
  94.     NULL,            /* *Bitmap    */
  95.     0,0,0,0,        /* Min/Max w,h    */
  96.     CUSTOMSCREEN        /* Type        */
  97. };
  98.  
  99. SHORT cmap[32] =
  100. {
  101. 0x000, 0xfff, 0xffd, 0xffb, 0xff9, 0xef8, 0xdf7, 0xde7,
  102. 0xdc7, 0xda7, 0xd87, 0xb87, 0xb85, 0xb83, 0xa84, 0x985,
  103. 0x886, 0x877, 0x857, 0x958, 0xa48, 0xb38, 0x938, 0x738,
  104. 0x648, 0x558, 0x468, 0x369, 0x36b, 0x36d, 0x45d, 0x55e
  105. };
  106.  
  107. main(argc,argv)
  108. int argc;
  109. char *argv[];
  110. {
  111.     setup();
  112.     drawstuff();
  113.     die(0);
  114. }
  115.  
  116. setup()
  117. {
  118.     if(!(GfxBase = OpenLibrary("graphics.library",0l)))
  119.         die(1);
  120.     if(!(IntuitionBase = OpenLibrary("intuition.library", 0l)))
  121.         die(2);
  122.     if(!(s = OpenScreen(&ss)))
  123.         die(4);
  124.     ww.Screen = s;
  125.     ww2.Screen = s;
  126.     if(!(w = OpenWindow(&ww2)))
  127.         die(5);
  128.     if(!(w2 = OpenWindow(&ww)))
  129.         die(6);
  130.  
  131.     /* Set up a buffer for the AreaMove, AreaDraw, and AreaEnd commands */
  132.  
  133.     InitArea(&myAreaInfo, areabuffer, 100l);
  134.     w->RPort->AreaInfo = &myAreaInfo;
  135.     tmpras.RasPtr = (BYTE *) AllocRaster(320l, 200l);
  136.     tmpras.Size = (long) RASSIZE(320l, 200l);
  137.     w->RPort->TmpRas = &tmpras;
  138.  
  139.     LoadRGB4(&s->ViewPort, cmap, 32L);
  140. }
  141.  
  142. drawstuff()
  143. {
  144.     struct RastPort *r;
  145.     register long loop;
  146.     long color, x[10], y[10], dx[10], dy[10];
  147.  
  148.  
  149.     for(loop = 0l; loop < 5l; loop++)
  150.     { /* init the position */
  151.         x[loop] = (long)random(w->Width - w->BorderLeft
  152.             - w->BorderRight) + w->BorderLeft;
  153.         y[loop] = (long)random(w->Height - w->BorderTop
  154.             - w->BorderBottom) + w->BorderTop;
  155.         dx[loop] = dy[loop] = 0L;
  156.     }
  157.     r = w->RPort;
  158.     msg = GetMsg(w2->UserPort);
  159.     while(msg->Class != CLOSEWINDOW)
  160.     {
  161.         if(msg)
  162.             ReplyMsg(msg);
  163.         msg = GetMsg(w2->UserPort);
  164.  
  165.         /* draw with 2 colors this time, and just one the next to */
  166.         /* get all 3 colors modified. */
  167.  
  168.         SetDrMd(r, JAM2);
  169.  
  170.         color += random(3) - 1;
  171.  
  172.         if(color < 1L) color = 1L;
  173.         if(color > 31L) color = 31L;
  174.  
  175.         SetAPen(r, color);
  176.  
  177.  
  178.         /* Do a 5 point polygon */
  179.         for(loop = 0l; loop < 5l; loop++)
  180.         {
  181.             dx[loop] += (long)random(3) - 1L;
  182.             dy[loop] += (long)random(3) - 1L;
  183.  
  184.             if(dy[loop] > 5L) dy[loop] = 5L;
  185.             if(dx[loop] > 5L) dx[loop] = 5L;
  186.             if(dy[loop] < -5L) dy[loop] = -5L;
  187.             if(dx[loop] < -5L) dx[loop] = -5L;
  188.  
  189.             if(x[loop] + dx[loop] >  w->Width - w->BorderRight
  190.             || x[loop] + dx[loop] <  w->BorderLeft)
  191.                 dx[loop] *= -1;
  192.  
  193.             if(y[loop] + dy[loop] >  w->Height - w->BorderBottom
  194.             || y[loop] + dy[loop]<  w->BorderTop)
  195.                 dy[loop] *= -1;
  196.  
  197.             x[loop] += dx[loop];
  198.             y[loop] += dy[loop];
  199.  
  200.             if(x[loop] > w->Width - w->BorderRight)
  201.                 x[loop] = w->Width - w->BorderRight;
  202.             if(x[loop] < w->BorderLeft)
  203.                 x[loop] = w->BorderLeft;
  204.             if(y[loop] > w->Height - w->BorderBottom)
  205.                 y[loop] = w->Height - w->BorderBottom;
  206.             if(y[loop] < w->BorderTop)
  207.                 y[loop] = w->BorderTop;
  208.  
  209.             if(loop == 0l)
  210.                 AreaMove(r, x[loop], y[loop]);
  211.             else
  212.                 AreaDraw(r, x[loop], y[loop]);
  213.         }
  214.         AreaEnd(r);
  215. WaitTOF();
  216.         ClipBlit(r, 0L, 10L, w2->RPort, 0L, 10L,
  217.             (long)w->Width, (long)w->Height-10L, 0xc0L);
  218.     }
  219.     if(msg)
  220.         ReplyMsg(msg);
  221. }
  222.  
  223. /****************************************************************
  224.  * I don't think this random number will set any speed records, but
  225.  * it is guaranteed to be as random as you can get.
  226.  ****************************************************************/
  227. random(max)
  228. int max;
  229. {
  230.     static ULONG num;
  231.     ULONG sec, mic;
  232.  
  233.     CurrentTime(&sec, &mic);
  234.     num *= sec;
  235.     num += mic;
  236.     while(num > 32000)
  237.         num = num >> 1;
  238.     return((int)(num % (ULONG)max));
  239. }
  240.  
  241. die(kind)
  242. int kind;
  243. {
  244.     static char *msgs[] = {    "",
  245.         /* err 1 */    "Unable to open graphics library.",
  246.         /* err 2 */    "Unable to open intuition library.",
  247.         /* err 3 */    "Unable to open a window.",
  248.                 "screen failed.",
  249.                 "window 1 failed.",
  250.                 "window 2 failed."
  251.                 };
  252.  
  253.     if(kind)        puts(msgs[kind]);
  254.     if(tmpras.RasPtr)    FreeRaster(tmpras.RasPtr,320l,200L);
  255.     if(w)            CloseWindow(w);
  256.     if(w2)            CloseWindow(w2);
  257.     if(s)            CloseScreen(s);
  258.     if(GfxBase)        CloseLibrary(GfxBase);
  259.     if(IntuitionBase)    CloseLibrary(IntuitionBase);
  260.     exit(kind);
  261. }
  262.  
  263.